﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TaskLib
{
    class ListViewItemComparer : IComparer
    {
        protected int column;
        protected SortOrder sortOrder;

        public ListViewItemComparer()
        {
            this.column = 0;
            sortOrder = SortOrder.Ascending;
        }

        public ListViewItemComparer(int column, SortOrder sortOrder)
        {
            this.column = column;
            this.sortOrder = sortOrder;
        }

        public int Compare(object x, object y)
        {
            int ret = -1;

            var item1 = x as ListViewItem;
            var item2 = y as ListViewItem;
            if (item1 != null && item2 != null)
            {
                ret = String.Compare(item1.SubItems[column].Text, item2.SubItems[column].Text);
                if (sortOrder == SortOrder.Descending)
                {
                    ret *= -1;
                }
            }
            return ret;
        }       
    }
}
